package com.xshang.tenant;

import com.zaxxer.hikari.HikariDataSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import javax.sql.DataSource;

@Service
public class DataSourceService {

    private final Logger log = LoggerFactory.getLogger(DataSourceService.class);

    private final DataSource dataSource;

    public DataSourceService(@Qualifier("tenantDataSource") DataSource dataSource) {
        this.dataSource = dataSource;
    }

    public void getCurrentTenantDataSource() {
        log.info(dataSource.getClass().getName());

        if (dataSource instanceof HikariDataSource) {
            HikariDataSource hikariDataSource = (HikariDataSource) dataSource;
            log.info("当前使用的数据源: {}", hikariDataSource.getJdbcUrl());
        }

        if (dataSource instanceof TenantDataSource) {
            TenantDataSource tenantDataSource = (TenantDataSource) dataSource;
            log.info("当前使用的数据源: {}", tenantDataSource.determineCurrentLookupKey());
            log.info(TenantDataSourceContext.get());
        }

    }


}
